/***************************************************************************
 *   Copyright (C) 2007 by Ivan Vasić                                      *
 *   ivasic@gmail.com                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "mysqlmodeluser.h"

#include "exceptions.h"

#include <QtCore>
#include <QtSql>

MySQLModelUser::MySQLModelUser(const QString id, QObject *parent)
		: ModelUser(id, parent)
{
	update();
}


MySQLModelUser::~MySQLModelUser()
{}

void MySQLModelUser::update()
{
	QSqlQuery query;
	query.prepare("SELECT * FROM User WHERE username = ?");
	query.bindValue(0, this->id(), QSql::In);
	query.exec();	
	
	if(query.lastError().type() != QSqlError::NoError)
		throw new DBException(query.lastError().text(), Exception::Normal, __FILE__, __FUNCTION__, __LINE__);
	
	if(!query.next())
	{
		m_isValid = FALSE;
		return;
	}
	
	m_isValid = TRUE;
	
	m_username = query.record().value("username").toString();
	m_password = query.record().value("password").toString();
	m_passkey = query.record().value("passkey").toString();
	m_email = query.record().value("email").toString();
	m_uploaded = query.record().value("uploaded").toULongLong();
	m_downloaded = query.record().value("downloaded").toULongLong();
	m_name = query.record().value("name").toString();
	m_joined = query.record().value("joined").toDateTime();
}

QStringList MySQLModelUser::getPropertiesMap() const
{
	QStringList ret;
	
	ret << "Username";
	ret << m_username;
	
	ret << "Password";
	ret << m_password;	
	
	ret << "Email";
	ret << m_email;
	
	///@TODO: Dodaj i ostale parametre	
	
	return ret;
}

void MySQLModelUser::remove()
{
	QSqlQuery query;
	query.prepare("DELETE FROM User WHERE username = ?");
	query.bindValue(0, this->id(), QSql::In);
	query.exec();
	
	if(query.lastError().type() != QSqlError::NoError)
		throw new DBException(query.lastError().text(), Exception::Normal, __FILE__, __FUNCTION__, __LINE__);
	
	m_isValid = FALSE;
}
